/*
 * File         : string.c
 * This file is part of POSIX-RTOS
 * COPYRIGHT (C) 2015 - 2016, DongHeng
 * 
 * Change Logs:
 * DATA             Author          Note
 * 2015-11-20       DongHeng        create
 */

#include "string.h"
/*@{*/

/**
  * memcpy - This function will copy memory content form source address to 
  *          destination  address.
  *
  * @param dest  the address of destination memory
  * @param src   the address of source memory
  * @param count the copied length of byte data
  *
  * @return the address of destination memory
  */
void* memcpy(void *dest, const void *src, size_t count)
{
    char *_d = (char *)dest, *_s = (char *)src;
    
    while (count--)
        *_d++ = *_s++;
    
    return dest;
}

/**
  * memset - This function will set the memory of the specified length to the 
  *          specified value.
  *
  * @param src   the address of source memory
  * @param ch    the specified value to be set
  * @param count the set length of byte data
  *
  * @return the address of source memory
  */
void* memset(void *src, int ch, size_t count)
{
    char *_s = (char *)src;
    
    while (count--)
        *_s++ = ch;
    
    return src;
}

/**
  * memcmp - This function will compare two memory block with specified length.
  *
  * @param s1    the source 1 string point to be compared
  * @param s2    the source 2 string point to be compared
  * @param count the length of the memory block
  *
  * @return the result
  */
int memcmp( const char *s1, const char *s2, size_t count )
{
    while (--count && *s1 == *s2)
        s1++, s2++;
  
    return (*s1 - *s2);
}

/**
  * strcmp - This function will compare two string without specified length.
  *
  * @param s1 the source 1 string point to be compared
  * @param s2 the source 2 string point to be compared
  *
  * @return the result
  */
int strcmp(const char *s1, const char *s2)
{
    while (*s1 && *s1 == *s2)
        s1++, s2++;
  
    return (*s1 - *s2);
}

/**
  * strncmp - This function will compare two string with specified length.
  *
  * @param s1    the source 1 string point to be compared
  * @param s2    the source 2 string point to be compared
  * @param count the maximum length of string to be compared
  *
  * @return the result
  */
int strncmp(const char *s1, const char *s2, size_t count)
{
    while (count-- && *s1 && *s1 == *s2)
        s1++, s2++;
  
    return (*s1 - *s2);
}

/**
  * strlen - This function will return the length of the string.
  *
  * @param src the source string
  *
  * @return the length of string
  */
int strlen( const char *src )
{
    const char *_s = src;
    
    for ( _s = src; *_s; ++_s );
    
    return (_s - src);
}

/**
  * strnlen - This function will return the length of the string with specified and limited 
  * length.
  *
  * @param src   the source string
  * @param count the maximum length to be checked
  *
  * @return the length of string with the specified and limited length
  */
int strnlen( const char *src, size_t count )
{
    const char *_s = src;
    
    for ( _s = src; count-- && *_s; ++_s );
    
    return (_s - src);
}

/**
  * strchr - This function will search the specified character
  *
  * @param src the source string
  * @param ch  the specified character 
  *
  * @return the point where the byte is the same as the specified character or 
  * return null if not find
  */
char* strchr( const char *str, int ch )
{
    while (*str && *str != ch)
        str++;
  
    return (*str == ch) ? (char *)str : NULL;
}

/**
  * strstr - This function will search the specified string length.
  *
  * @param s1 the source string
  * @param s2 the target string 
  *
  * @return the point where the source string is the same as the target string 
  *         or return null if not find
 */
char* strstr(const char *s1, const char *s2)
{
    int l1, l2;
    
    l2 = strlen(s2) - 1;
    l1 = strlen(s1);
    
    while (l1 >= l2)
    {
        if (!strncmp(s1, s2, l2))
            return (char *)s1;
        ++s1;
        --l1;
    }
    
    return NULL;
}

/*@}*/

